package com.yuexunit.operationman.nbsy.module.BeforeAnesthesiaVisit.ui;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.BeforeAnesthesiaVisit.model.impl.BeforeAnesthesiaModel;
import com.yuexunit.operationman.nbsy.module.BeforeAnesthesiaVisit.presenter.IBeforeAnesthesiaPresenter;
import com.yuexunit.operationman.nbsy.module.BeforeAnesthesiaVisit.presenter.impl.BeforeAnesthesiaPresenter;
import com.yuexunit.operationman.nbsy.module.BeforeAnesthesiaVisit.ui.view.IBeforeAnesthesiaView;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/8/4 0004.
 */

public class Act_BeforeAnesthesiaSign extends BaseKJActivity implements IBeforeAnesthesiaView {

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.postoperative_diagnosis)
    EditText postoperativeDiagnosis;
    @Bind(R.id.anesthetics)
    EditText anesthetics;
    @Bind(R.id.dosage)
    EditText dosage;
    @Bind(R.id.anesthesia)
    EditText anesthesia;
    @Bind(R.id.surgeon)
    EditText surgeon;
    @Bind(R.id.transfuse_drug)
    EditText transfuseDrug;
    @Bind(R.id.complaint)
    EditText complaint;
    @Bind(R.id.other)
    EditText other;

    LoadingDialog loadingDialog;
    IBeforeAnesthesiaPresenter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_beforeanesthesiasign);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        loadingDialog = new LoadingDialog(this);
        presenter = new BeforeAnesthesiaPresenter(this, new BeforeAnesthesiaModel());
        setTitle();
        setContent();
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getPatientName());
        if (AppConfig.BEFOREANSETHESIA.getObject().getVisit().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.BEFOREANSETHESIA.getObject().getVisit().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.BEFOREANSETHESIA.getObject().getVisit().getInHospitalNo());
        tvPatientDisease.setText(PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DISEASE));
    }

    private void setContent() {
        if(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getVisitId() != null)
        {
           if(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getPatientHeight()!=null)
           {
               postoperativeDiagnosis.setText(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getPatientHeight());
           }
            if(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getPatientWeight()!=null)
            {
                anesthetics.setText(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getPatientWeight());
            }
            if(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getBp()!=null)
            {
                dosage.setText(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getBp());
            }
            if(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getRespire()!=null)
            {
                anesthesia.setText(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getRespire());
            }
            if(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getPulse()!=null)
            {
                surgeon.setText(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getPulse());
            }
            if(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getTemperature()!=null)
            {
                transfuseDrug.setText(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getTemperature());
            }
            if(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getComplain()!=null)
            {
                complaint.setText(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getComplain());
            }
            if(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getOtherDesc()!=null)
            {
                other.setText(AppConfig.BEFOREANSETHESIA.getObject().getVisit().getOtherDesc());
            }
        }
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.sure:
                insertContent();
                break;
        }
    }

    private void insertContent() {
        if (postoperativeDiagnosis.getText().length() > 0) {
            AppConfig.BEFOREANSETHESIA.getObject().getVisit().setPatientHeight(postoperativeDiagnosis.getText().toString());
        }
        if (anesthetics.getText().length() > 0) {
            AppConfig.BEFOREANSETHESIA.getObject().getVisit().setPatientWeight(anesthetics.getText().toString());
        }
        if (dosage.getText().length() > 0) {
            AppConfig.BEFOREANSETHESIA.getObject().getVisit().setBp(dosage.getText().toString());
        }
        if (anesthesia.getText().length() > 0) {
            AppConfig.BEFOREANSETHESIA.getObject().getVisit().setRespire(anesthesia.getText().toString());
        }
        if (surgeon.getText().length() > 0) {
            AppConfig.BEFOREANSETHESIA.getObject().getVisit().setPulse(surgeon.getText().toString());
        }
        if (transfuseDrug.getText().length() > 0) {
            AppConfig.BEFOREANSETHESIA.getObject().getVisit().setTemperature(transfuseDrug.getText().toString());
        }
        if (complaint.getText().length() > 0) {
            AppConfig.BEFOREANSETHESIA.getObject().getVisit().setComplain(complaint.getText().toString());
        }
        if (other.getText().length() > 0) {
            AppConfig.BEFOREANSETHESIA.getObject().getVisit().setOtherDesc(other.getText().toString());
        }

        if (AppConfig.BEFOREANSETHESIA.getObject().getVisit().getVisitId() == null) {
            Map<String, String> params = new HashMap<>();
            params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.BEFOREANSETHESIA));
            presenter.insertBeforeAnesthesiaDate(params);
        } else {
            Map<String, String> params = new HashMap<>();
            params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
            params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.BEFOREANSETHESIA));
            presenter.updateBeforeAnesthesiaDate(params);
        }
    }

    @Override
    public void insertBeforeAnesthesiaDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void updateBeforeAnesthesiaDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }

    /**
     * 通过dispatchTouchEvent每次ACTION_DOWN事件中动态判断非EditText本身区域的点击事件，然后在事件中进行屏蔽。然后当点击不是EditText时候，进行隐藏键盘操作
     *
     * @param ev
     * @return
     */

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {

        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件\
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
}
